import 'dart:math';

import 'package:equatable/equatable.dart';
import 'package:flutter_soundmagus/util/ble_helper.dart';
import 'package:json_annotation/json_annotation.dart';

import 'model.dart';

part 'XoverItemModel.g.dart';

String filterTypeStr(int type) {
  final typeStrs = ['Butter-W', 'Bessel', 'LINK-RIL'];
  return typeStrs[type];
}

@JsonSerializable()
class XoverItemHighLowModel {
  /// 是否选中*/
  bool selected;

  bool isBypass;

  /// 0Butter 1Bessel 2Link-Ril*/
  int type;

  /// 0Butter 1Bessel 2Link-Ril*/
  static final typeStrs = ['Butter-W', 'Bessel', 'LINK-RIL'];
  String getTypeStr() {
    return typeStrs[type];
  }

  /// 0:6 1:12 2:24 3:48*/
  /// 斜率
  int slope;

  ///频率
  int freq;

  ///斜率列表值
  List<String> get dbs {
    return ["6", "12", "16", "18", "24"];//把斜率的SLOPE改成：6.12.16.18.24
    if (type == 2) {
      return ["6", "12", "24", "36", "48"]; //宁克
    }
    return ["6", "12", "18", "24", "36", "48"];
  }

  XoverItemHighLowModel(
      {this.selected = false,
      this.isBypass = false,
      this.type = 2,
      this.slope = 0,
      this.freq = 0});

  XoverItemHighLowModel copyWith(
      {bool? selected,
      bool? isBypass,
      int? type,
      int? slope,
      int? freq,
      List<String>? dbs}) {
    this.selected = selected ?? this.selected;
    this.isBypass = isBypass ?? this.isBypass;
    this.type = type ?? this.type;
    this.slope = slope ?? this.slope;
    this.freq = freq ?? this.freq;

    return this;
  }

  factory XoverItemHighLowModel.fromJson(Map<String, dynamic> json) =>
      _$XoverItemHighLowModelFromJson(json);

  Map<String, dynamic> toJson() => _$XoverItemHighLowModelToJson(this);
}

@JsonSerializable(explicitToJson: true)
class XoverItemModel {
  XoverItemHighLowModel high;
  XoverItemHighLowModel low;

  XoverItemModel({XoverItemHighLowModel? high, XoverItemHighLowModel? low})
      : this.high = high ?? XoverItemHighLowModel(freq: 20, slope: 0),
        this.low = low ?? XoverItemHighLowModel(freq: 20000, slope: 0);

  XoverItemModel copyWith(
      {XoverItemHighLowModel? high, XoverItemHighLowModel? low}) {
    this.high = high ?? this.high;
    this.low = low ?? this.low;
    return this;
  }

  factory XoverItemModel.fromJson(Map<String, dynamic> json) =>
      _$XoverItemModelFromJson(json);

  Map<String, dynamic> toJson() => _$XoverItemModelToJson(this);
}

///滤波类型
enum PassType {
  ///高通
  high,

  ///低通
  low,

  ///全通
  all
}

@JsonSerializable(explicitToJson: true)
class XoverModel with ModelResetable, CmdStringable {
  ///滤波类型
  PassType passType;

  ///通道联调配置
  ChConfig chConfig;

  ///当前选中的通道、0-7单选 8-10联调
  int get selectCH {
    return chConfig.selectCh;
  }

  List<XoverItemModel> items;

  XoverModel(
      {List<XoverItemModel>? items,
      this.passType = PassType.high,
      ChConfig? chConfig})
      : this.items = items ?? List.generate(8, (index) => XoverItemModel()),
        this.chConfig = chConfig ?? ChConfig();

  XoverModel copyWith(
      {List<XoverItemModel>? items, PassType? passType, ChConfig? chConfig}) {
    this.items = items ?? this.items;
    this.passType = passType ?? this.passType;
    this.chConfig = chConfig ?? this.chConfig;
    return this;
  }

  ///更新xover item
  XoverModel updateItem(
      {bool isHigh = false,
      bool? selected,
      bool? isBypass,
      int? type,
      int? slope,
      int? freq,
      List<String>? dbs}) {
    var itemModel = this.itemModel();
    var hlModel = (isHigh ? itemModel.high : itemModel.low).copyWith(
        freq: freq,
        slope: slope,
        type: type,
        isBypass: isBypass,
        selected: selected);

    if (isHigh) {
      itemModel = itemModel.copyWith(high: hlModel);
    } else {
      itemModel = itemModel.copyWith(low: hlModel);
    }

    var items = this.items.map((e) => e.copyWith()).toList()
      ..[selectCH] = itemModel;

    //如果是联调，找同组通道同步数据
    var chObj = chConfig.chs[selectCH];
    if (chObj.isGroup) {
      var relativeCH = chConfig.getRelativeCh();
      items[relativeCH] = XoverItemModel.fromJson(itemModel.toJson());
    }

    var model = copyWith(items: items);
    return model;
  }

  XoverItemModel itemModel({int? selectCH}) {
    var i = selectCH ?? this.selectCH;
    i = min(i, items.length - 1);
    return items[i];
  }

  factory XoverModel.fromJson(Map<String, dynamic> json) =>
      _$XoverModelFromJson(json);

  Map<String, dynamic> toJson() => _$XoverModelToJson(this);

  @override
  void reset() {
    items = List.generate(8, (index) => XoverItemModel());
    chConfig = ChConfig();
  }

  @override
  String toCmdStr() {
    var str = '';
    for (var idx = 0; idx < items.length; idx++) {
      var obj = items[idx];
      ZFXoverMsg msg = ZFXoverMsg();
      msg.ch.value = idx + 0xc1;
      msg.xielv.value = obj.high.slope;
      msg.freq.value = obj.high.freq;
      msg.type.value = obj.high.type;
      msg.cmd.value = 0xdd;
      str += '${msg.toHexString()}\n';

      msg = ZFXoverMsg();
      msg.ch.value = idx + 0xc1;
      msg.xielv.value = obj.low.slope;
      msg.freq.value = obj.low.freq;
      msg.type.value = obj.low.type;
      msg.cmd.value = 0xde;
      str += '${msg.toHexString()}\n';
    }
    return str;
  }
}

extension XoverModelExt on XoverModel {
  ///同步通道所有数据到另外联调的通道
  void syncData({int? ch}) {
    var chi = ch ?? selectCH, relaCh = chConfig.getRelativeCh(ch: chi);
    items[relaCh] = XoverItemModel.fromJson(itemModel().toJson());
  }
}
